//
//  ViewController.m
//  CommandPattern
//
//  Created by 颖 on 2017/11/28.
//  Copyright © 2017年 SUIRUI. All rights reserved.
//

#import "ViewController.h"
#import "ControlCell.h"
#import "Command.h"
#import "NoCommand.h"
#import "LightOnCommand.h"
#import "LightOffCommand.h"
#import "Light.h"
#import "Stereo.h"
#import "StereoOnWithCDCommand.h"
#import "StereoOffCommand.h"
#import "BaseCommand.h"

#define CELLID @"Control"

@interface ViewController ()<UITableViewDataSource,LastCommandDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) NSMutableArray<id<Command>> *onCommands;

@property (nonatomic, strong) NSMutableArray<id<Command>> *offCommands;

@property (nonatomic, strong)  id<Command> undoCommand;

@property (nonatomic, strong) NSMutableArray<NSString *> *slotNames;

@property (nonatomic, strong) Light *livingRoomLight;

@property (nonatomic, strong) Light *kitchenLight;

@property (nonatomic, strong) Stereo *livingRoomStereo;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
//    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self livingRoomLight];
    [self kitchenLight];
    
    [self livingRoomStereo];
    
    [self onCommands];
    [self offCommands];
  
    self.tableView.rowHeight = 44;

    [self.tableView registerNib:[UINib nibWithNibName:@"ControlCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:CELLID];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _slotNames.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ControlCell *cell = [tableView dequeueReusableCellWithIdentifier:CELLID forIndexPath:indexPath];
    NSUInteger slot = indexPath.row;
    BaseCommand *onCo = _onCommands[slot];
    BaseCommand *offCo = _offCommands[slot];
    [cell setOnCommand:onCo offCommand:offCo slotName:_slotNames[slot]];
    onCo.lastComDelegate = self;
    offCo.lastComDelegate = self;
    return cell;
}

- (void)callbackLastCommand:(id<Command>)command{
    NSUInteger index = [_onCommands containsObject:command] ? [_onCommands indexOfObject:command] : [_offCommands indexOfObject:command];
    id<Command> undoCom = [_onCommands containsObject:command] ? [_offCommands objectAtIndex:index] : [_onCommands objectAtIndex:index];
    _undoCommand = undoCom;
    
}

- (IBAction)cancel:(id)sender {
    [_undoCommand execute];
}

-(NSMutableArray *)onCommands{
    if (_onCommands == nil) {
        _onCommands = [NSMutableArray new];
        _slotNames = [NSMutableArray new];
        LightOnCommand *ligthOn = [[LightOnCommand alloc] initWith:_livingRoomLight];
        [_onCommands addObject:ligthOn];
        [_slotNames addObject:_livingRoomLight.name];
        
        ligthOn = [[LightOnCommand alloc] initWith:_kitchenLight];
        [_onCommands addObject:ligthOn];
        [_slotNames addObject:_kitchenLight.name];
        
        StereoOnWithCDCommand *stereoOn = [[StereoOnWithCDCommand alloc] initWithStereo:_livingRoomStereo];
        [_onCommands addObject:stereoOn];
        [_slotNames addObject:_livingRoomStereo.stereoName];
        NoCommand *noCommand = [NoCommand new];
        for (int i = 0; i < 5; ++i) {
            [_onCommands addObject:noCommand];
            [_slotNames addObject:@"NULL"];
        }
    }
    return _onCommands;
}

- (NSMutableArray *)offCommands{
    if (_offCommands == nil) {
        _offCommands = [NSMutableArray new];
        LightOffCommand *lightOff = [[LightOffCommand alloc] initWithLight:_livingRoomLight];
        [_offCommands addObject:lightOff];
        
        lightOff = [[LightOffCommand alloc] initWithLight:_kitchenLight];
        [_offCommands addObject:lightOff];
        
        StereoOffCommand *stereoOff = [[StereoOffCommand alloc] initWithStereo:_livingRoomStereo];
        [_offCommands addObject:stereoOff];
        NoCommand *noCommand = [NoCommand new];
        for (int i = 0; i < 5; ++i) {
            [_offCommands addObject:noCommand];
        }
    }
    return _offCommands;
}

- (Light *)livingRoomLight{
    if (_livingRoomLight == nil) {
        _livingRoomLight = [[Light alloc] initWithName:@"卧室灯"];
    }
    return _livingRoomLight;
}

- (Light *)kitchenLight{
    if (_kitchenLight == nil) {
        _kitchenLight = [[Light alloc] initWithName:@"厨房灯"];
    }
    return _kitchenLight;
}

- (Stereo *)livingRoomStereo{
    if (_livingRoomStereo == nil) {
        _livingRoomStereo = [[Stereo alloc] initWithName:@"卧室音响"];
    }
    return _livingRoomStereo;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
